{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "13e61b48",
   "metadata": {},
   "source": [
    "# Numpy"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "874aa1af",
   "metadata": {},
   "source": [
    "- Numpy 安装： pip install numpy\n",
    "\n",
    "- 导入：`import numpy as np`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b1e56915",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ad647f4d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'1.20.3'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.__version__"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8a11663a",
   "metadata": {},
   "source": [
    "- **Numpy 最重要的一个特点是其 N 维数组对象 ndarray （本质是ndarray)，它是一系列同类型数据的集合。**\n",
    "\n",
    "- 多维矩阵\n",
    "    - 1维数组看做向量 vector -- 点和直线\n",
    "    - 2维数组看做 2维矩阵 -- 表和平面\n",
    "    - 3维数组看做 3维矩阵\n",
    "    \n",
    "- Numpy 常用方法 — 矩阵和随机数生成\n",
    "\n",
    "    - `np.arange` \n",
    "    - `np.linspace` \n",
    "    - `np.zeros` \n",
    "    - `np.ones`\n",
    "    - `np.full`\n",
    "    - `np.random` "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0c51c425",
   "metadata": {},
   "source": [
    "## Numpy 数组"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c9a6850a",
   "metadata": {},
   "source": [
    "### Python list & array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b1ceb1aa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Python list\n",
    "a=list(range(10))\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "5c6531a3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4, 'Hey', 6, 7, 8, 9]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[5]='Hey'\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "33b38afc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array('i', [0, 1, 2, 3, 89, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Python array\n",
    "\n",
    "'''\n",
    "Arrays are sequence types and behave very much like lists, except that \n",
    "the type of objects stored in them is constrained. \n",
    "\n",
    "The type is specified at object creation time by using a type code, \n",
    "which is a single character.\n",
    "'''\n",
    "  \n",
    "import array \n",
    "arr = array.array('i',list(range(10)))  # i stands for integer\n",
    "arr[4]=89\n",
    "arr"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8bb1a1b9",
   "metadata": {},
   "source": [
    "### 为什么要用numpy\n",
    "\n",
    "\n",
    "- Python中提供了list容器，可以当作数组使用。但列表中的元素可以是任何对象，因此列表中保存的是对象的指针，这样一来，为了保存一个简单的列表[1,2,3]。就需要三个指针和三个整数对象。对于数值运算来说，这种结构显然不够高效。\n",
    "\n",
    "- Python虽然也提供了array模块，但其只支持一维数组，不支持多维数组，也没有各种运算函数。因而不适合数值运算。\n",
    "\n",
    "- NumPy的出现弥补了这些不足。\n",
    "\n",
    "《Python科学计算》\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "86ea6012",
   "metadata": {},
   "source": [
    "### np.arange\n",
    "\n",
    "- np.arange()函数返回一个有终点和起点的固定步长的排列, 类型为numpy.ndarray, 可被当作向量使用\n",
    "\n",
    "- 参数个数情况： np.arange()函数分为一个参数，两个参数，三个参数三种情况 \n",
    "\n",
    "    - 一个参数时，参数值为终点，起点取默认值0，步长取默认值1。 \n",
    "\n",
    "    - 两个参数时，第一个参数为起点，第二个参数为终点，步长取默认值1。 \n",
    "\n",
    "    - 三个参数时，第一个参数为起点，第二个参数为终点，第三个参数为步长。其中步长支持小数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "424b2376",
   "metadata": {},
   "outputs": [],
   "source": [
    "a=np.arange(1,10,0.5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "9e440897",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e9c1fcb2",
   "metadata": {},
   "source": [
    "### numpy.array   \n",
    "\n",
    "- 将 list 转化为数组，numpy.ndarray类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7c993f9e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=np.array(list(range(10)))\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "71a568be",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('int32')"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 类型 \n",
    "type(a)\n",
    "\n",
    "# 查看元素类型 nparray.dtype\n",
    "a.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "f4f569c0",
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "invalid literal for int() with base 10: 'Hey'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-24-159b15a347b8>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# 可以改变元素取值， 但是不可以改变类型\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0ma\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'Hey'\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      3\u001b[0m \u001b[0ma\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: 'Hey'"
     ]
    }
   ],
   "source": [
    "# 可以改变元素取值， 但是不可以改变类型\n",
    "a[4]='Hey'\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "84e554e4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0, 15,  2,  3, 78,  5,  6,  7,  8,  9])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 整数类型的ndarray，放入小数，截取小数点前整数\n",
    "a.dtype\n",
    "a[1]=15.89\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "9b703ed0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('float64')"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=np.array([1,2,3],dtype=float)\n",
    "a.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "25d88d1d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [2, 4]])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array([[1,2],[2,4]])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78233c52",
   "metadata": {},
   "source": [
    "#### numpy.ndarray 的特点\n",
    "\n",
    "- 1 方便处理多维度数组或者矩阵之间的运算\n",
    "\n",
    "- 2 在处理多维数组或者矩阵，运算效率高"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "d9d6e9ec",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 2, 12, 36, 80, 150, 252, 392, 576, 810]"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算一个列表(list(range(n)))内元素的平方，加上相同列表内元素的立方\n",
    "# 写成函数形式，参数为n, 首先使用列表表达式操作\n",
    "\n",
    "def python_test(n):\n",
    "    #a=[i**2 for i in range(n)]\n",
    "    #b=[i**3 for i in range(n)]\n",
    "    c=[i**2+i**3 for i in range(n)]\n",
    "    return c\n",
    "\n",
    "python_test(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "50a7caca",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  0,   2,  12,  36,  80, 150, 252, 392, 576, 810], dtype=int32)"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def numpy_test(n):\n",
    "    a=np.arange(n)**2\n",
    "    b=np.arange(n)**3\n",
    "    c=a+b\n",
    "    return c\n",
    "\n",
    "numpy_test(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "557420bb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Wall time: 131 ms\n"
     ]
    }
   ],
   "source": [
    "# 测试运行时间\n",
    "\n",
    "%time res = python_test(100000)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "df3f30aa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Wall time: 3.99 ms\n"
     ]
    }
   ],
   "source": [
    "%time res = numpy_test(100000)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d8974743",
   "metadata": {},
   "source": [
    "## 矩阵和随机数的生成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "ab2ee07f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# np.zeros(num)\n",
    "np.zeros(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "c303b537",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[0., 0.],\n",
       "        [0., 0.],\n",
       "        [0., 0.]],\n",
       "\n",
       "       [[0., 0.],\n",
       "        [0., 0.],\n",
       "        [0., 0.]],\n",
       "\n",
       "       [[0., 0.],\n",
       "        [0., 0.],\n",
       "        [0., 0.]]])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# np.zeros(shape=(n1,n2)) \n",
    "np.zeros(shape=(3,3,2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "a8f726b9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# np.ones()\n",
    "np.ones(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "e50c49a1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[1., 1., 1., 1.],\n",
       "        [1., 1., 1., 1.],\n",
       "        [1., 1., 1., 1.]],\n",
       "\n",
       "       [[1., 1., 1., 1.],\n",
       "        [1., 1., 1., 1.],\n",
       "        [1., 1., 1., 1.]],\n",
       "\n",
       "       [[1., 1., 1., 1.],\n",
       "        [1., 1., 1., 1.],\n",
       "        [1., 1., 1., 1.]],\n",
       "\n",
       "       [[1., 1., 1., 1.],\n",
       "        [1., 1., 1., 1.],\n",
       "        [1., 1., 1., 1.]]])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ones((4,3,4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "ccc12ae3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[9, 9],\n",
       "       [9, 9],\n",
       "       [9, 9],\n",
       "       [9, 9],\n",
       "       [9, 9]])"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# np.full()\n",
    "np.full((5,2),9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "e29a8d28",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 0., 0.],\n",
       "       [0., 1., 0.],\n",
       "       [0., 0., 1.]])"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# np.identity()  创建单位矩阵\n",
    "np.identity(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "1ec080ab",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 0, 0],\n",
       "       [0, 2, 0],\n",
       "       [0, 0, 7]])"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# np.diag([1,2,3,4]) 根据一个向量创建斜对角方阵\n",
    "np.diag([3,2,7])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "7c6c98cd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.        ,  0.51282051,  1.02564103,  1.53846154,  2.05128205,\n",
       "        2.56410256,  3.07692308,  3.58974359,  4.1025641 ,  4.61538462,\n",
       "        5.12820513,  5.64102564,  6.15384615,  6.66666667,  7.17948718,\n",
       "        7.69230769,  8.20512821,  8.71794872,  9.23076923,  9.74358974,\n",
       "       10.25641026, 10.76923077, 11.28205128, 11.79487179, 12.30769231,\n",
       "       12.82051282, 13.33333333, 13.84615385, 14.35897436, 14.87179487,\n",
       "       15.38461538, 15.8974359 , 16.41025641, 16.92307692, 17.43589744,\n",
       "       17.94871795, 18.46153846, 18.97435897, 19.48717949, 20.        ])"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 等差数列 np.linspace(start, stop, num=50, endpoint=True)\n",
    "# 在指定的间隔[\"start\",\"stop\"]内均匀地返回“num”个等间距的样本。\n",
    "\n",
    "np.linspace(0,20,40)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "541a061c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x25f561275f8>]"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD8CAYAAABn919SAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAHRVJREFUeJzt3Xl4XXW97/H3d2ce2qRp0tI2TdOZFkpbGkqhikApMonocYCjCF6u5To8gooeFa96hvvo8R5Ffe49alEEjiCijJdBLZOFQktT7DzQkqZT0mZqkzRpkj387h97p8SaNtPee+3h83rYz1p77ZWuz6Lw6epvr8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FgStJg5y+AAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "#指数图\n",
    "import matplotlib.pyplot as plt\n",
    "x=np.arange(-5,5,0.1)\n",
    "y=np.power(2,x)   # y=2^x \n",
    "#print(y)\n",
    "plt.plot(x,y)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bb29672a",
   "metadata": {},
   "source": [
    "## 生成随机数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "4d573d5b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# np.random.randint(low, high=None)\n",
    "# 返回一个随机整型数，范围从低（包括）到高（不包括），即[low, high)。\n",
    "# 如果没有写参数high的值，则返回[0,low)的值。\n",
    "\n",
    "np.random.randint(19)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "d03206d6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[16,  2],\n",
       "       [18,  2]])"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# np.random.randint(low, high=None, size=None)\n",
    "np.random.randint(0,20,(2,2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8b972b5e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "0e19d58a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 9, 16],\n",
       "       [ 4, 10]])"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# set seed \n",
    "# np.random.seed()\n",
    "np.random.seed(200)\n",
    "np.random.randint(0,20,(2,2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "0548ee87",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[0.57393148, 0.82162058, 0.11615185, 0.43578581, 0.72702957,\n",
       "         0.65220774],\n",
       "        [0.96747567, 0.62448817, 0.32588307, 0.70680958, 0.6087204 ,\n",
       "         0.1232923 ]],\n",
       "\n",
       "       [[0.18722056, 0.20979547, 0.30387949, 0.51735222, 0.37444118,\n",
       "         0.97089086],\n",
       "        [0.81661962, 0.75236148, 0.05637445, 0.56773504, 0.48357037,\n",
       "         0.95808299]]])"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# np.random.random() # 0到1 的浮点数\n",
    "np.random.random((2,2,6))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "6217a096",
   "metadata": {},
   "outputs": [],
   "source": [
    "# np.random.random((3, 5)) # 0到1 的浮点数\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "904e3a7c",
   "metadata": {},
   "source": [
    "## Numpy 数组（ndarray）的属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "44bd2799",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# ndarray.ndim : 维度 (属性)\n",
    "A = np.ones((3,4))\n",
    "A\n",
    "A.ndim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "f222ee40",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# ndarray.shape:每一个维度的信息  （属性）\n",
    "A.shape[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "b77f09fc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# ndarray.size: 表示包含的元素总数 (属性)\n",
    "A.size"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78018024",
   "metadata": {},
   "source": [
    "## Numpy 数组（ndarray）的基础操作"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "23dd1e6c",
   "metadata": {},
   "source": [
    "### 修改维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "31fc5da7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3, 4],\n",
       "       [5, 6, 7, 8, 9]])"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# ndarray.reshape() 修改维度\n",
    "a=np.arange(10)\n",
    "b=a.reshape(-1,5)\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "b2566dde",
   "metadata": {},
   "outputs": [],
   "source": [
    "# ndarray.reshape(n,-1)  or ndarray.reshape(-1,n)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "31fb63ee",
   "metadata": {},
   "source": [
    "### 取值操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "aad5bc06",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11],\n",
       "       [12, 13, 14, 15]])"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A=np.arange(16).reshape(4,4)\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "14fa68fc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A[(0,0)]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f290d98",
   "metadata": {},
   "source": [
    "### 切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "ec4e6f95",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(10)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "86c8d317",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4])"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[0:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "483f3293",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4])"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "d13938f1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[5:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "d66bf0c9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 2, 4, 6])"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[0:8:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "e84ffa6f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 2, 4, 6, 8])"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[::2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "2923d75a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[::-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c8d5e055",
   "metadata": {},
   "source": [
    "### 二维数组的切片操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "98e4b029",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3,  4],\n",
       "       [ 5,  6,  7,  8,  9],\n",
       "       [10, 11, 12, 13, 14],\n",
       "       [15, 16, 17, 18, 19]])"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = np.arange(20).reshape(4,-1)\n",
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "95781aab",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 2, 4],\n",
       "       [5, 7, 9]])"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 前2行,前3列\n",
    "X[0:2,::2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "8c5f1981",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[19, 18, 17, 16, 15],\n",
       "       [14, 13, 12, 11, 10],\n",
       "       [ 9,  8,  7,  6,  5],\n",
       "       [ 4,  3,  2,  1,  0]])"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X[::-1,::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "id": "70f80bc4",
   "metadata": {},
   "outputs": [],
   "source": [
    "#X[::-1,::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "56b29616",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  5, 10, 15],\n",
       "       [ 1,  6, 11, 16],\n",
       "       [ 2,  7, 12, 17],\n",
       "       [ 3,  8, 13, 18],\n",
       "       [ 4,  9, 14, 19]])"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#转置：ndarray.T\n",
    "X.T"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3ae378a6",
   "metadata": {},
   "source": [
    "### 矩阵的合并"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "0beaef3b",
   "metadata": {},
   "outputs": [],
   "source": [
    "X1 = np.array([[22, 168],\n",
    "               [18,187]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3e2c1af4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.],\n",
       "       [1.]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X2 = np.array([[0],\n",
    "               [1]])\n",
    "\n",
    "X3 = np.ones((2,1))\n",
    "X3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "474b8b95",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 22., 168.,   0.,   1.],\n",
       "       [ 18., 187.,   1.,   1.]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.concatenate([X1,X2,X3],axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d2210b4",
   "metadata": {},
   "source": [
    "- np.concatenate((a1, a2, …), axis=0) 数组拼接, 传入的参数必须是一个多个数组的元组或者列表\n",
    "\n",
    "- 拼接的方向，默认是 axis = 0，对数组对象进行 行拼接（axis= 1 列拼接）；\n",
    "\n",
    "- 传入的数组必须具有相同的形状，这里的相同的形状可以满足在拼接方向axis轴上数组间的形状一致即可"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e7b7f656",
   "metadata": {},
   "source": [
    "### 聚合操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "id": "4217994e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3,  4],\n",
       "       [ 5,  6,  7,  8,  9],\n",
       "       [10, 11, 12, 13, 14]])"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = np.arange(15).reshape(3, 5)\n",
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "5a8d75c5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([10, 35, 60])"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(X,axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 150,
   "id": "4f2e84c1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([190, 207])"
      ]
     },
     "execution_count": 150,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(X,axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "id": "063627fa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([10, 11, 12, 13, 14])"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.max(X,axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "id": "9143310c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5., 6., 7., 8., 9.])"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(X,axis=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0aa26d9c",
   "metadata": {},
   "source": [
    "## 读取和存储数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f505d834",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "#changing_rate=np.load('changing_rate.npy').tolist()\n",
    "# np.save   np.load\n",
    "# np.savetxt    np.loadtxt   csv格式"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a34b2f46",
   "metadata": {},
   "source": [
    "# Pandas"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3fec152f",
   "metadata": {},
   "source": [
    "## Series 一维数组\n",
    "\n",
    "### Seris的创建和对象获取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "59c6530f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    1\n",
      "1    2\n",
      "2    3\n",
      "3    4\n",
      "dtype: int64\n",
      "a    1\n",
      "b    2\n",
      "c    3\n",
      "d    4\n",
      "dtype: int64\n",
      "e    1\n",
      "f    2\n",
      "g    3\n",
      "h    4\n",
      "dtype: int64\n",
      "0    0\n",
      "1    1\n",
      "2    2\n",
      "3    3\n",
      "4    4\n",
      "dtype: int64\n",
      "0    0\n",
      "1    1\n",
      "2    2\n",
      "3    3\n",
      "4    4\n",
      "5    5\n",
      "6    6\n",
      "7    7\n",
      "8    8\n",
      "9    9\n",
      "dtype: int32\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "# 创建Series对象\n",
    "# 如果省略index参数，会创建默认索引，索引值为[0,1,...,len(data)-1]\n",
    "seri1 = pd.Series([1,2,3,4]) \n",
    "print(seri1)\n",
    "\n",
    "# 自己创建索引\n",
    "# seril = pd.Series([1,2,3,4], index = ['a','b','c','d'])\n",
    "seri1 = pd.Series([1,2,3,4], index = list('abcd'))\n",
    "print(seri1)\n",
    "\n",
    "# 将dict转换为series\n",
    "data_dict = {'e':1,'f':2, 'g':3, 'h':4}\n",
    "seri1 = pd.Series(data_dict)\n",
    "print(seri1)\n",
    "\n",
    "# 将range(), np.arange() 转化为series\n",
    "seri1 = pd.Series(range(5))\n",
    "print(seri1)\n",
    "\n",
    "seri1 = pd.Series(np.arange(10))\n",
    "print(seri1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "fb8eae01",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4]\n",
      "Index(['a', 'b', 'c', 'd'], dtype='object')\n",
      "[('a', 1), ('b', 2), ('c', 3), ('d', 4)]\n"
     ]
    }
   ],
   "source": [
    "# 获取series内容\n",
    "seri1 = pd.Series([1,2,3,4], index = list('abcd'))\n",
    "print(seri1.values)\n",
    "\n",
    "# 获取series索引\n",
    "print(seri1.index)\n",
    "\n",
    "# 获取series索引和值对\n",
    "print(list(seri1.items()))\n",
    "# print(list(seri1.iteritems()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "id": "0bcd6219",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "use location:  3\n",
      "use index:  3\n",
      "use index：  a    1\n",
      "c    3\n",
      "dtype: int64\n",
      "use location：  a    1\n",
      "c    3\n",
      "dtype: int64\n",
      "index slice：  a    1\n",
      "b    2\n",
      "c    3\n",
      "dtype: int64\n",
      "location slice：  a    1\n",
      "b    2\n",
      "c    3\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "# 选取数据\n",
    "seri1 = pd.Series([1,2,3,4], index = list('abcd'))\n",
    "## 使用位置获取数据\n",
    "print('use location: ',seri1[2])\n",
    "## 使用索引获取数据\n",
    "print('use index: ',seri1['c'])\n",
    "\n",
    "## 获取多个数据\n",
    "print('use index： ',seri1[['a','c']])\n",
    "print('use location： ',seri1[[0,2]])\n",
    "\n",
    "# 使用切片获取数据\n",
    "print('index slice： ',seri1['a':'c'])  #左右都包含\n",
    "print('location slice： ',seri1[0:3])   # 做包含右不包含"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "107440cb",
   "metadata": {},
   "source": [
    "### 修改数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "id": "548b9403",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    1\n",
      "b    2\n",
      "c    3\n",
      "d    4\n",
      "dtype: int64\n",
      "o    1\n",
      "p    2\n",
      "q    3\n",
      "r    4\n",
      "dtype: int64\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "o     1\n",
       "p     2\n",
       "q     3\n",
       "r     4\n",
       "s    40\n",
       "dtype: int64"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 修改索引 \n",
    "seri1 = pd.Series([1,2,3,4], index = list('abcd'))\n",
    "print(seri1)\n",
    "seri1.index = list('opqr')\n",
    "print(seri1) \n",
    "\n",
    "# reindex \n",
    "seri1.reindex(index = list('achfk'))\n",
    "# print(seri1)\n",
    "seri1.reindex(index=list('opqrs'),fill_value=40)\n",
    "# print(seri1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "id": "575affef",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "o    1\n",
      "p    2\n",
      "q    3\n",
      "r    4\n",
      "dtype: int64\n",
      "o      1\n",
      "p    100\n",
      "q      3\n",
      "r      4\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "# 修改值\n",
    "print(seri1)\n",
    "seri1[1]=100\n",
    "print(seri1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "id": "2bf47d11",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    0\n",
      "b    1\n",
      "c    2\n",
      "d    3\n",
      "e    4\n",
      "dtype: int64\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "d    3\n",
       "e    4\n",
       "dtype: int64"
      ]
     },
     "execution_count": 130,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 删除值, drop\n",
    "seri1 = pd.Series(range(5),index = list('abcde'))\n",
    "print(seri1)\n",
    "seri1.drop(['a','b','c'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "201d80e2",
   "metadata": {},
   "source": [
    "### 计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "id": "08a1f9fc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "BJ    1\n",
      "SH    2\n",
      "GZ    3\n",
      "HZ    4\n",
      "dtype: int64\n",
      "BJ    11\n",
      "SH    12\n",
      "JN    13\n",
      "WH    14\n",
      "dtype: int64\n",
      "BJ    12.0\n",
      "GZ     NaN\n",
      "HZ     NaN\n",
      "JN     NaN\n",
      "SH    14.0\n",
      "WH     NaN\n",
      "dtype: float64\n",
      "BJ    1\n",
      "SH    2\n",
      "GZ    3\n",
      "dtype: int64\n",
      "BJ    2\n",
      "SH    4\n",
      "GZ    6\n",
      "HZ    8\n",
      "dtype: int64\n",
      "BJ     1\n",
      "SH     4\n",
      "GZ     9\n",
      "HZ    16\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "seri1 = pd.Series([1,2,3,4],index=['BJ','SH','GZ','HZ']) \n",
    "seri2 = pd.Series([11,12,13,14],index=['BJ','SH','JN','WH']) \n",
    "\n",
    "print(seri1)\n",
    "print(seri2) \n",
    "print(seri1+seri2)\n",
    "\n",
    "# 过滤数据\n",
    "print(seri1[seri1 <= 3])\n",
    "# 标量乘法  \n",
    "print(seri1*2) \n",
    "# numpy 函数 \n",
    "print(np.square(seri1)) "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c162e1cd",
   "metadata": {},
   "source": [
    "## DataFrame 二维数组"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d7c6029c",
   "metadata": {},
   "source": [
    "### DataFrame 创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 172,
   "id": "71e1cb32",
   "metadata": {},
   "outputs": [],
   "source": [
    "# data 数据\n",
    "# index 行索引\n",
    "# columns 列索引\n",
    "df = pd.DataFrame(data = np.random.randint(0,10,(4,4)),\n",
    "                 index = [1,2,3,4],\n",
    "                 columns = list('abcd'))\n",
    "\n",
    "df\n",
    "\n",
    "# 使用字典生成Dataframe\n",
    "# 字典的key为列索引 \n",
    "data_dict = {'id':['101','102','103','104'], 'grade':[100,90,80,95], 'height':[165,198,178,168]}\n",
    "df2 = pd.DataFrame(data_dict, index = ['wang','may','john','kate'])\n",
    "df2\n",
    "\n",
    "df3 = pd.DataFrame.from_dict(data_dict) \n",
    "df3\n",
    "\n",
    "# Series 转成DataFrame \n",
    "# 索引相同直接合并, 索引不同对应缺少值NaN \n",
    "data_dict2 = {'id': pd.Series(['101','102','103'],index = list('abc')),\n",
    "              'age': pd.Series([20,30,23,25], index = list('abcd')),\n",
    "              'height': pd.Series([176,159,165], index = list('abe'))\n",
    "}\n",
    "df4 = pd.DataFrame(data_dict2)\n",
    "df4\n",
    "\n",
    "# Dataframe转成字典\n",
    "df4.to_dict()\n",
    "# 列索引 外层字典的key \n",
    "# 行索引 内层字典的key\n",
    "\n",
    "# 建立空DataFrame\n",
    "df5 = pd.DataFrame(columns = list('abcd'))\n",
    "# df5.iloc[0]=[1,2,3,4]\n",
    "# df5"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bf1d960d",
   "metadata": {},
   "source": [
    "### DataFrame 基本属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 182,
   "id": "c0ec5c52",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       id  grade  height\n",
      "wang  101    100     165\n",
      "may   102     90     198\n",
      "john  103     80     178\n",
      "kate  104     95     168\n",
      "(4, 3)\n",
      "2\n",
      "['id', 'grade', 'height']\n",
      "['wang', 'may', 'john', 'kate']\n",
      "id        object\n",
      "grade      int64\n",
      "height     int64\n",
      "dtype: object\n",
      "[['101' 100 165]\n",
      " ['102' 90 198]\n",
      " ['103' 80 178]\n",
      " ['104' 95 168]]\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Index: 4 entries, wang to kate\n",
      "Data columns (total 3 columns):\n",
      " #   Column  Non-Null Count  Dtype \n",
      "---  ------  --------------  ----- \n",
      " 0   id      4 non-null      object\n",
      " 1   grade   4 non-null      int64 \n",
      " 2   height  4 non-null      int64 \n",
      "dtypes: int64(2), object(1)\n",
      "memory usage: 128.0+ bytes\n",
      "None\n",
      "            grade      height\n",
      "count    4.000000    4.000000\n",
      "mean    91.250000  177.250000\n",
      "std      8.539126   14.908052\n",
      "min     80.000000  165.000000\n",
      "25%     87.500000  167.250000\n",
      "50%     92.500000  173.000000\n",
      "75%     96.250000  183.000000\n",
      "max    100.000000  198.000000\n",
      "       id  grade  height\n",
      "wang  101    100     165\n",
      "may   102     90     198\n",
      "       id  grade  height\n",
      "john  103     80     178\n",
      "kate  104     95     168\n"
     ]
    }
   ],
   "source": [
    "data_dict = {'id':['101','102','103','104'], 'grade':[100,90,80,95], 'height':[165,198,178,168]}\n",
    "df2 = pd.DataFrame(data_dict, index = ['wang','may','john','kate'])\n",
    "print(df2)\n",
    "\n",
    "print(df2.shape) #行列数\n",
    "print(df2.ndim) #维度\n",
    "\n",
    "print(df2.columns.tolist())  #列索引 \n",
    "print(df2.index.tolist())  # 行索引 \n",
    "\n",
    "print(df2.dtypes) #数据类型 \n",
    "print(df2.values) #提取数据\n",
    "\n",
    "print(df2.info()) # 数据信息 \n",
    "print(df2.describe())  # 对数值变量的一些分布信息\n",
    "\n",
    "print(df2.head(2)) # 头部, 默认5行数据,  \n",
    "print(df2.tail(2)) # 尾部, "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ccc7f704",
   "metadata": {},
   "source": [
    "### 获取行列的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 196,
   "id": "48cc0acf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       id  grade  height\n",
      "wang  101    100     165\n",
      "may   102     90     198\n",
      "john  103     80     178\n",
      "kate  104     95     168\n",
      "wang    100\n",
      "may      90\n",
      "john     80\n",
      "kate     95\n",
      "Name: grade, dtype: int64\n",
      "<class 'pandas.core.series.Series'>\n",
      "wang    100\n",
      "may      90\n",
      "john     80\n",
      "kate     95\n",
      "Name: grade, dtype: int64\n",
      "      grade  height\n",
      "wang    100     165\n",
      "may      90     198\n",
      "john     80     178\n",
      "kate     95     168\n",
      "       id  grade  height\n",
      "wang  101    100     165\n",
      "       id  grade  height\n",
      "may   102     90     198\n",
      "john  103     80     178\n",
      "       id  grade\n",
      "may   102     90\n",
      "john  103     80\n"
     ]
    }
   ],
   "source": [
    "# 方法1：基础索引，直接引用，使用df[]\n",
    "# NOTE: df[] 只能进行列选择，或行选择，不能同时放入多行多列选择\n",
    "\n",
    "# 列\n",
    "data_dict = {'id':['101','102','103','104'], 'grade':[100,90,80,95], 'height':[165,198,178,168]}\n",
    "df2 = pd.DataFrame(data_dict, index = ['wang','may','john','kate'])\n",
    "print(df2)\n",
    "\n",
    "print(df2['grade'])  # 获取一列，得到的对象为Series\n",
    "print(type(df2['grade']))\n",
    "\n",
    "print(df2.grade)\n",
    "\n",
    "# 获取多列\n",
    "print(df2[['grade','height']]) #得到的对象为DataFrame\n",
    "\n",
    "# 获取一行\n",
    "print(df2[0:1])\n",
    "# 获取多行\n",
    "print(df2[1:3])\n",
    "\n",
    "# 获取多行多列\n",
    "print(df2[1:3][['id','grade']])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 215,
   "id": "105effbd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       id  grade  height\n",
      "wang  101    100     165\n",
      "may   102     90     198\n",
      "john  103     80     178\n",
      "kate  104     95     168\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "wang    100\n",
       "may      90\n",
       "john     80\n",
       "kate     95\n",
       "Name: grade, dtype: int64"
      ]
     },
     "execution_count": 215,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方法2：使用df.loc[]， 通过标签索引获得数据 \n",
    "\n",
    "data_dict = {'id':['101','102','103','104'], 'grade':[100,90,80,95], 'height':[165,198,178,168]}\n",
    "df2 = pd.DataFrame(data_dict, index = ['wang','may','john','kate'])\n",
    "print(df2)\n",
    "\n",
    "# 获取一行数据\n",
    "df2.loc['john']\n",
    "\n",
    "#获取某一行列位置的数据\n",
    "df2.loc['john','grade']\n",
    "\n",
    "#获取一行，多列数据\n",
    "df2.loc['john',['grade','height']]\n",
    "\n",
    "# 获取多行多列\n",
    "df2.loc[['john','may'],['grade','height']]\n",
    "df2.loc['wang':'john',:]\n",
    "\n",
    "# 获取一列\n",
    "df2.loc[:,'grade']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 218,
   "id": "db1ec483",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       id  grade  height\n",
      "wang  101    100     165\n",
      "may   102     90     198\n",
      "john  103     80     178\n",
      "kate  104     95     168\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "id        104\n",
       "grade      95\n",
       "height    168\n",
       "Name: kate, dtype: object"
      ]
     },
     "execution_count": 218,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方法3：使用df.iloc[]， 通过位置获得数据\n",
    "data_dict = {'id':['101','102','103','104'], 'grade':[100,90,80,95], 'height':[165,198,178,168]}\n",
    "df2 = pd.DataFrame(data_dict, index = ['wang','may','john','kate'])\n",
    "print(df2)\n",
    "\n",
    "# 获取一行\n",
    "df2.iloc[0]\n",
    "\n",
    "# 获取多行\n",
    "df2.iloc[0:3]\n",
    "df2.iloc[[0,3]]\n",
    "\n",
    "# 获取一列\n",
    "df2.iloc[:,2]\n",
    "\n",
    "# 某一个值 \n",
    "df2.iloc[1,0]\n",
    "\n",
    "# 最后一行\n",
    "df2.iloc[-1,:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 221,
   "id": "4ea7693b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>grade</th>\n",
       "      <th>height</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>wang</th>\n",
       "      <td>101</td>\n",
       "      <td>100</td>\n",
       "      <td>165</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>john</th>\n",
       "      <td>103</td>\n",
       "      <td>99</td>\n",
       "      <td>178</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>kate</th>\n",
       "      <td>104</td>\n",
       "      <td>95</td>\n",
       "      <td>168</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>may</th>\n",
       "      <td>102</td>\n",
       "      <td>90</td>\n",
       "      <td>198</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       id  grade  height\n",
       "wang  101    100     165\n",
       "john  103     99     178\n",
       "kate  104     95     168\n",
       "may   102     90     198"
      ]
     },
     "execution_count": 221,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 修改 \n",
    "df2.loc['john','grade'] = 99\n",
    "df2\n",
    "\n",
    "# 排序 \n",
    "# by 字段 列 \n",
    "# ascending 默认排序方式：升序 , 降序 ascending= False\n",
    "df2.sort_values(by='grade',ascending=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c5eaeaa1",
   "metadata": {},
   "source": [
    "### 修改index, columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 257,
   "id": "19154b1c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       id  grade  height\n",
      "wang  101    100     165\n",
      "may   102     90     198\n",
      "john  103     80     178\n",
      "kate  104     95     168\n",
      "Index(['wang', 'may', 'john', 'kate'], dtype='object')\n",
      "             id  p_cupl  q_cupl\n",
      "student_id                     \n",
      "101         101     100     165\n",
      "102         102      90     198\n",
      "103         103      80     178\n",
      "104         104      95     168\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>104</th>\n",
       "      <th>104</th>\n",
       "      <th>95</th>\n",
       "      <th>168</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>student_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>101</th>\n",
       "      <td>101</td>\n",
       "      <td>100</td>\n",
       "      <td>165</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102</th>\n",
       "      <td>102</td>\n",
       "      <td>90</td>\n",
       "      <td>198</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>103</th>\n",
       "      <td>103</td>\n",
       "      <td>80</td>\n",
       "      <td>178</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>104</th>\n",
       "      <td>104</td>\n",
       "      <td>95</td>\n",
       "      <td>168</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "104         104   95  168\n",
       "student_id               \n",
       "101         101  100  165\n",
       "102         102   90  198\n",
       "103         103   80  178\n",
       "104         104   95  168"
      ]
     },
     "execution_count": 257,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_dict = {'id':['101','102','103','104'], 'grade':[100,90,80,95], 'height':[165,198,178,168]}\n",
    "df2 = pd.DataFrame(data_dict, index = ['wang','may','john','kate'])\n",
    "print(df2)\n",
    "\n",
    "# 直接替换 index，columns\n",
    "print(df2.index)\n",
    "df2.index = list('abcd')\n",
    "df2\n",
    "\n",
    "df2.columns = list('opq')\n",
    "df2\n",
    "\n",
    "# 使用rename\n",
    "# 传入函数，调整索引规则\n",
    "def index_adjust(x):  # 使用函数，自定义调整规则\n",
    "    #print(x)\n",
    "    return x + '2022'\n",
    "def columns_adjust(x):\n",
    "    return x + '_cupl'\n",
    "df2.rename(index = index_adjust, columns = columns_adjust, inplace = True)  \n",
    "# inplace: 默认为False, 不在原dataframe上修改，返回新的dataframe; True: 在原dataframe上修改\n",
    "df2\n",
    "\n",
    "# rename 也可以传入字典，更改某个或某几个index 或columns\n",
    "df2.rename(index={'a2022':'a'},columns= {'o_cupl': 'id'},inplace= True)\n",
    "df2\n",
    "\n",
    "# 将列转化为索引, 指定一列为索引\n",
    "df2.set_index('id', inplace=True,drop=False)\n",
    "df2.index.name = 'student_id'\n",
    "print(df2)\n",
    "\n",
    "# 将行索引转换为列\n",
    "df2.reset_index()  # drop = True: delete index\n",
    "\n",
    "\n",
    "# 将行转化为列名\n",
    "df3 = df2.set_axis(df2.iloc[3],axis = 1)\n",
    "df3.columns.name = None\n",
    "df3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cc66d1fb",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
